package cn.com.blackboard.DataModule4B2.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cm.com.blackboard.DataModule4B2.customizeConfigUtils.BatchImportUtil;
import cn.com.blackboard.DataModule4B2.BBDataUtils.UserUtil;
import cn.com.blackboard.DataModule4B2.dao.StudentInfoMapper;
import cn.com.blackboard.DataModule4B2.model.StudentInfo;
import cn.com.blackboard.DataModule4B2.model.TeacherInfo;
import cn.com.blackboard.DataModule4B2.service.DepartmentService;
import cn.com.blackboard.DataModule4B2.service.StudentService;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel;
import cn.com.blackboard.DataModule4B2.util.Condition;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel.Status;

@Service("StudentService")
public class StudentServiceImpl implements StudentService{

	private static final Logger logger = LoggerFactory.getLogger(StudentServiceImpl.class);
	
	@Autowired
	private StudentInfoMapper studentInfoMapper;

	@Override
	public List<StudentInfo> getUsers() {
		// TODO Auto-generated method stub
		return studentInfoMapper.selectByCondition(Condition.SearchAll);
	}

	@Override
	public List<StudentInfo> getUsers(String jsonCondition) {	
		Condition condition = Condition.ParseCondition(jsonCondition);
		String conditionStr = Condition.RenderConditoin(condition);
		return studentInfoMapper.selectByCondition(conditionStr);
	}

	@Override
	public int insert(StudentInfo userInfo) {
		// TODO Auto-generated method stub
		return studentInfoMapper.insert(userInfo);
	}

	@Override
	public int updateStatus(String userId) {
		// TODO Auto-generated method stub
		return studentInfoMapper.updateRecordImportedByUserId(userId);
	}
	
	@Override
	public List<BatchResultModel> importStudent(List<StudentInfo> studentInfoLst){
		logger.info("start to import student");
		List<BatchResultModel> list = new ArrayList<BatchResultModel>();
		int count = 0;
		for(StudentInfo si:studentInfoLst){
			BatchResultModel batchResultModel = new BatchResultModel();
			batchResultModel.setModelClsName(TeacherInfo.class.getName());
			batchResultModel.setModelClsAttr("userId-firstName");
			batchResultModel.setValue(si.getUserId()+"-"+si.getFirstName());
			if(si.getIsimported().equals("N")){
				Status status = UserUtil.importStudent(si);
				batchResultModel.setStatus(status);
				count += status.isSuccess()?1:0;
				if(status.isSuccess())
					updateStatus(si.getUserId());
			}
			else{			
				Status status = UserUtil.updateStudent(si);
				batchResultModel.setStatus(status);
				count += status.isSuccess()?1:0;
			}
			list.add(batchResultModel);
		}
		logger.info("student need to be imported is "+studentInfoLst.size()+" the num imported indeed is "+count);
    	return list;
	}

	@Override
	public List<BatchResultModel> batchImportStudent() {
		String conditionStr = BatchImportUtil.getBatchFilterCondition(StudentInfo.class.getName());
		List<StudentInfo> studentInfoLst = studentInfoMapper.selectByCondition(conditionStr);
		List<BatchResultModel> list = importStudent(studentInfoLst);
		return list;
	}
}
